package weaver.interfaces.holyview.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.govern.constant.FieldName;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.holyview.duss.util.DataUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 公告发布流程
 * @ModifyLog:
 **/
/*
 *@Description: 差旅费报销传凭证接口
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2020/12/10
 */
public class WeaTravelExpenseReimbursementAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        int userid = requestInfo.getRequestManager().getUserId();
        JSONObject jsonData = new JSONObject();
        try {
            ResourceComInfo comInfo = new ResourceComInfo();
            String sql = "select id,zfrq,sqr,hjkm,jdrybm,jdbmbm,yfdl,xxje,jtfsehj,zsfsehj,qtfysehj,jdsxhj," +
                    "bccxyxzf,bccxje,zfzh from " + tablename + " where requestid=?";
            rs.executeQuery( sql, requestid );
            baseBean.writeLog( sql, requestid );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );
            jsonData.put( "zfrq", Util.null2String( rs.getString( "zfrq" ) ) );
            String sqr = Util.null2String( rs.getString( "sqr" ) );//申请人
            String sqrName = comInfo.getLastname(sqr);
            String hjkm = Util.null2String( rs.getString( "hjkm" ) );//会计科目
            String jdrybm = Util.null2String( rs.getString( "jdrybm" ) );//金蝶人员编码
            String jdbmbm = Util.null2String( rs.getString( "jdbmbm" ) );//金蝶部门编码
            String yfdl = Util.null2String( rs.getString( "yfdl" ) );//研发大类
            String xxje = Util.null2String( rs.getString( "xxje" ) );//小写金额
            String jtfsehj = Util.null2String( rs.getString( "jtfsehj" ) );//交通费税额合计
            String zsfsehj = Util.null2String( rs.getString( "zsfsehj" ) );//住宿费税额合计
            String qtfysehj = Util.null2String( rs.getString( "qtfysehj" ) );//其他费用税额合计
            String jdsxhj = Util.null2String( rs.getString( "jdsxhj" ) );//金蝶所需合计
            String bccxyxzf = Util.null2String( rs.getString( "bccxyxzf" ) );//本次冲销金额+银行支付金额
            String bccxje = Util.null2String( rs.getString( "bccxje" ) );//本次冲销金额
            String yhzfje = Util.null2String( rs.getString( "yhzfje" ) );//银行支付金额
            String zfzh = Util.null2String( rs.getString( "zfzh" ) );//银行支付账号
            //单据体：FEntity
            JSONArray FEntity = new JSONArray();
            /*单据体第一行*/
            if(true){
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqrName + "报销" + "-差旅费" );
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", hjkm );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //核算维度
                JSONObject FDetailID = new JSONObject();
                //金蝶部门编码
                JSONObject FDETAILID__FFLEX5 = new JSONObject();
                FDETAILID__FFLEX5.put( "FNumber", jdbmbm );
                FDetailID.put( "FDETAILID__FFLEX5", FDETAILID__FFLEX5 );
                //金蝶大类
                JSONObject FDETAILID__FF100002 = new JSONObject();
                FDETAILID__FF100002.put( "FNumber", yfdl);
                FDetailID.put( "FDETAILID__FF100002", FDETAILID__FF100002 );
                //金蝶人员编码
                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put( "FNumber", jdrybm );
                FDetailID.put( "FDETAILID__FFLEX7", FDETAILID__FFLEX7 );
                FEntityJSON.put( "FDetailID", FDetailID );
                //借方金额
                FEntityJSON.put( "FDEBIT", jdsxhj );

                FEntity.add( FEntityJSON );
            }
            /*单据体第二行*/
            //交通费
            String sql1 = "select id from " + tablename + "_dt1 where mainid = ?";
            rs.executeQuery( sql1, mainid );
            if (rs.next()) {
                JSONObject FEntityJSON = new JSONObject();
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", "2221.01.01.02" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //借方金额
                FEntityJSON.put( "FDEBIT", jtfsehj );
                FEntity.add( FEntityJSON );
            }
            /*单据体第三行*/
            //住宿费
            String sql2 = "select fphm from " + tablename + "_dt2 where mainid = ? and fplx=0";
            rs.executeQuery( sql1, mainid );
            while (rs.next()) {
                String fphm = Util.null2String( rs.getString( "fphm" ) );//发票号码
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqrName + "报销" + "-差旅费-" + fphm + "#" );
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", "2221.01.01.01" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //借方金额
                FEntityJSON.put( "FDEBIT", zsfsehj );
                FEntity.add( FEntityJSON );
            }
            /*单据体四行*/
            //其他费用
            String sql4 = "select fphm from " + tablename + "_dt4 where mainid = ? and fplx=0";
            rs.executeQuery( sql4, mainid );
            while (rs.next()) {
                String fphm = Util.null2String( rs.getString( "fphm" ) );//发票号码
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqrName + "报销" + "-差旅费-" + fphm + "#" );
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", "2221.01.01.01" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //借方金额
                FEntityJSON.put( "FDEBIT", qtfysehj );
                FEntity.add( FEntityJSON );
            }
            /*单据体五行*/
            if (true){
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqrName + "报销" + "-差旅费" );
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", "2241.03" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //核算维度
                JSONObject FDetailID = new JSONObject();
                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put( "FNumber", jdrybm );
                FDetailID.put( "FDETAILID__FFLEX7", FDETAILID__FFLEX7 );
                FEntityJSON.put( "FDetailID", FDetailID );
                //贷方金额
                FEntityJSON.put( "FCREDIT", xxje );
                FEntity.add( FEntityJSON );
            }
            /*单据体六行*/
            if(true){
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqrName + "报销" + "-差旅费" );
                //科目编码
                JSONObject FACCOUNTID3 = new JSONObject();
                FACCOUNTID3.put( "FNumber", "2241.03" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID3 );
                //核算维度
                JSONObject FDetailID = new JSONObject();

                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put( "FNumber", jdrybm );
                FDetailID.put( "FDETAILID__FFLEX7", FDETAILID__FFLEX7 );
                FEntityJSON.put( "FDetailID", FDetailID );
                //贷方金额
                FEntityJSON.put( "FCREDIT", bccxyxzf );
                FEntity.add( FEntityJSON );
            }
            /*单据体七行*/
            if(true){
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqr + "报销" + "-差旅费" );
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", "1221.03" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //核算维度
                JSONObject FDetailID = new JSONObject();

                JSONObject FDETAILID__FFLEX7 = new JSONObject();
                FDETAILID__FFLEX7.put( "FNumber", jdrybm );
                FDetailID.put( "FDETAILID__FFLEX7", FDETAILID__FFLEX7 );
                FEntityJSON.put( "FDetailID", FDetailID );
                //贷方金额
                FEntityJSON.put( "FCREDIT", bccxje );
                FEntity.add( FEntityJSON );
            }
            /*单据体八行*/
            if(true) {
                JSONObject FEntityJSON = new JSONObject();
                //摘要
                FEntityJSON.put( "FEXPLANATION", sqrName + "报销" + "-差旅费" );
                //科目编码
                JSONObject FACCOUNTID = new JSONObject();
                FACCOUNTID.put( "FNumber", "1221.01" );
                FEntityJSON.put( "FACCOUNTID", FACCOUNTID );
                //核算维度
                JSONObject FDetailID = new JSONObject();
                JSONObject FDETAILID__FF100006 = new JSONObject();
                FDETAILID__FF100006.put( "FNumber", DataUtil.getYhzhlb(zfzh));
                FDetailID.put( "FDETAILID__FF100006", FDETAILID__FF100006 );
                FEntityJSON.put( "FDetailID", FDetailID );
                //贷方金额
                FEntityJSON.put( "FCREDIT", yhzfje );
                FEntity.add( FEntityJSON );
            }
            jsonData.put( "FEntity", FEntity );
            baseBean.writeLog( "jsonData=" + jsonData.toJSONString() );

            String result = DataUtil.passCertificate( jsonData );
            baseBean.writeLog( "result=" + result.toString() );
            DataUtil.saveCallInterLog( "凭证", "Draft", result, requestid, userid );
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
